$(function(){
	var param = $("#tabContent > .tab-pane.active").data(); //获取上级页面的参数
	paramTabPage[tabId] = jQuery.extend(true, {}, param);
	$("[role='tabpanel']").each(function(i,n){
		$(n).addClass('testCon');
	});
	$(document).ajaxStop(function(){
	  drgUI.tips();
	})
	var quotas = [];	
	if (param.compareType == "none") {
		 quotas = [ {
			code : "InCaseCnt",
			name : "入组病例数",
			isLink : "1"
		}, {
			code : "TimeEff",
			name : "时间消耗指数",
			isLink : "0"
		}, {
			code : "FeeEff",
			name : "费用消耗指数",
			isLink : "0"
		}, {
			code : "CMI",
			name : "CMI值",
			isLink : "0"
			} ]
	} else {
		quotas = [
				{
					code : "InCaseCnt",
					name : "入组病例数",
					isLink : "1"
				},
				{
					code : "TimeEff",
					name : "时间消耗指数",
					isLink : "0"
				},
				{
					code : "FeeEff",
					name : "费用消耗指数",
					isLink : "0"
				},
				{
					code : "CMI",
					name : "CMI值",
					isLink : "0"
				},
				{
					code : "ExcessCnt_TF",
					name : (param.compareType == 'manage' ? "费用及住院日超标[绩效]病例数"
							: (param.compareType == 'std' ? "费用及住院日超标[标杆]病例数"
									: "费用及住院日超标[院内均值]病例数")),
					isLink : "1"
				},// 根据不同比较类型显示不同描述
				{
					code : "SpecificCnt_TF",
					name : "费用及住院日特异病例数",
					isLink : "1"
				}, {
					code : "TimeEff_TF",
					name : "矫正时间消耗指数",
					isLink : "0"
				}, {
					code : "FeeEff_TF",
					name : "矫正费用消耗指数",
					isLink : "0"
				} ]
	}
	
	var depQuotas = [];	
	if (param.compareType == "none") {
		depQuotas = [ {
			code : "InCaseCnt",
			name : "入组病例数",
			isLink : "1"
		}, {
			code : "TimeEff",
			name : "时间消耗指数",
			isLink : "0"
		}, {
			code : "FeeEff",
			name : "费用消耗指数",
			isLink : "0"
		}, {
			code : "CMI",
			name : "CMI值",
			isLink : "0"
			} ]
	} else {
		depQuotas = [
				{
					code : "InCaseCnt",
					name : "入组病例数",
					isLink : "1"
				},
				{
					code : "TimeEff",
					name : "时间消耗指数",
					isLink : "0"
				},
				{
					code : "TimeEffcc",
					name : (param.compareType == 'manage' ? "时间消耗指数[绩效]"
							: (param.compareType == 'std' ? "时间消耗指数[标杆]"
									: "时间消耗指数[院内均值]")),
					value: param.typeOne,
					isLink : "0"
				},
				{
					code : "FeeEff",
					name : "费用消耗指数",
					isLink : "0"
				},
				{
					code : "FeeEffcc",
					name : (param.compareType == 'manage' ? "费用消耗指数[绩效]"
							: (param.compareType == 'std' ? "费用消耗指数[标杆]"
									: "费用消耗指数[院内均值]")),
					value: param.typeTwo,
					isLink : "0"
				},
				{
					code : "CMI",
					name : "CMI值",
					isLink : "0"
				},
				{
					code : "ExcessCnt_TF",
					name : (param.compareType == 'manage' ? "费用及住院日超标[绩效]病例数"
							: (param.compareType == 'std' ? "费用及住院日超标[标杆]病例数"
									: "费用及住院日超标[院内均值]病例数")),
					isLink : "1"
				},// 根据不同比较类型显示不同描述
				{
					code : "SpecificCnt_TF",
					name : "费用及住院日特异病例数",
					isLink : "1"
				}, {
					code : "TimeEff_TF",
					name : "矫正时间消耗指数",
					isLink : "0"
				}, {
					code : "FeeEff_TF",
					name : "矫正费用消耗指数",
					isLink : "0"
				} ]
	}
	
	var legend = [param.depName];
	var xdata_1 = [];
	var xdata_2 = [];
	var seriesdata_1 = [];
	var seriesdata_2 = [];
	var ajaxCount = quotas.length*3;
	getTableData("Base", "dep", "hosp",function(data){
		var columns = [];
		var datas = [];
		datas = datas.concat(data);
		concatTableData(depQuotas, datas, "depQuota_TimeEffFeeEff-table", columns, "dep", "hosp",function(table){
			var param_t = drgUI.setAjaxData(tabId);//页面参数
			param_t.monitorType = "dep";
			getTrendData(param_t, "TimeEff", function(data_0){
				var seriedata_1 = {type: "line", clickable:false,smooth: true, symbol:"none", name: param.depName, data:[]};
				for(o in data_0){
					xdata_1.push(data_0[o].ddesc);
					seriedata_1.data.push(data_0[o].QuotaValue?data_0[o].QuotaValue:0);
				}
				seriesdata_1.push(seriedata_1);
				loadTrendChart("时间消耗指数", xdata_1, seriesdata_1, legend, $("#"+tabId+" #lineChart-1"));
				loadTrendChart("时间消耗指数", xdata_1, seriesdata_1, legend, $("#"+tabId+" #lineChart-3"));
			})
			getTrendData(param_t, "FeeEff", function(data_1){
				var seriedata_2 = {type: "line", clickable:false,smooth: true, symbol:"none", name: param.depName, data:[]};
				for(o in data_1){
					xdata_2.push(data_1[o].ddesc);
					seriedata_2.data.push(data_1[o].QuotaValue?data_1[o].QuotaValue:0);
				}
				seriesdata_2.push(seriedata_2);
				loadTrendChart("费用消耗指数", xdata_2, seriesdata_2, legend, $("#"+tabId+" #lineChart-2"));
				loadTrendChart("费用消耗指数", xdata_2, seriesdata_2, legend, $("#"+tabId+" #lineChart-4"));
			})
		});
		
	})

	getTableData("Base", "clinic", "dep",function(data){
		var columns = [{"data": "ddesc"}];
		var datas = [];
		datas = datas.concat(data);
		concatTableData(quotas, datas, "groupTable-table", columns, "clinic", "dep",function(table, oSetting){
			var seriesdata_1_copy = jQuery.extend(true, [], seriesdata_1);
			var seriesdata_2_copy = jQuery.extend(true, [], seriesdata_2);
			var legend_copy = jQuery.extend(true, [], legend);
			var aoData = oSetting.aoData;
			table.find("tbody tr").each(function(i,n){
				$(n).find("td:last a").unbind();
				$(n).find("td:last a").click(function(){
					var rowIndex = $(this).attr("data-rowindex");
					if($("#"+tabId+" #lineChart-1 img").size()>0 || $("#"+tabId+" #lineChart-2 img").size()>0) return;
					if(seriesdata_1_copy.length<=0) seriesdata_1_copy = jQuery.extend(true, [], seriesdata_1);
					if(seriesdata_2_copy.length<=0) seriesdata_2_copy = jQuery.extend(true, [], seriesdata_2);
					var aData = aoData[rowIndex]._aData;
					var ccode = aData.ccode;
					var ddesc=aData.ddesc;
					if($(this).find("i").hasClass("trend-link")){
						$("#"+tabId+" #lineChart-1").html('<img src="images/pageloading.gif" />');
						$("#"+tabId+" #lineChart-2").html('<img src="images/pageloading.gif" />');
						array_remove(legend_copy, ddesc);
						array_removeAttr(seriesdata_1_copy, "name", ccode);
						array_removeAttr(seriesdata_2_copy, "name", ccode);
						loadTrendChart("时间消耗指数", xdata_1, seriesdata_1_copy, legend_copy, $("#"+tabId+" #lineChart-1"));
						loadTrendChart("费用消耗指数", xdata_2, seriesdata_2_copy, legend_copy, $("#"+tabId+" #lineChart-2"));
						$(this).find("i").removeClass("trend-link");
					}else{
						$("#"+tabId+" #lineChart-1").html('<img src="images/pageloading.gif" />');
						$("#"+tabId+" #lineChart-2").html('<img src="images/pageloading.gif" />');
						var param_t = drgUI.setAjaxData(tabId);//页面参数
						param_t.monitorType = "clinic";
						param_t.pargroupName = ccode;
						legend_copy.push(ddesc);
						getTrendData(param_t, "TimeEff", function(data_0){
							var seriedata_avgdays = {type: "line", smooth: true, symbol:"none", name: ccode, data:[]};
							for(o in data_0){
								seriedata_avgdays.data.push(data_0[o].QuotaValue?data_0[o].QuotaValue:0);
							}
							seriesdata_1_copy.push(seriedata_avgdays);
							loadTrendChart("时间消耗指数", xdata_1, seriesdata_1_copy, legend_copy, $("#"+tabId+" #lineChart-1"));
						})
						getTrendData(param_t, "FeeEff", function(data_1){
							var seriedata_1 = {type: "line", smooth: true, symbol:"none", name: ccode, data:[]};
							for(o in data_1){
								seriedata_1.data.push(data_1[o].QuotaValue?data_1[o].QuotaValue:0);
							}
							seriesdata_2_copy.push(seriedata_1);
							loadTrendChart("费用消耗指数", xdata_2, seriesdata_2_copy, legend_copy, $("#"+tabId+" #lineChart-2"));
						})
						$(this).find("i").addClass("trend-link");
					}
				})
				table.find("i.trend-link").click();
			})
		});
	})
	getTableData("Base", "drgs","dep", function(data){
		var columns = [{"data": "ccode",
			"render": function(obj, type, full){
				return "<p><span class='drgtips' id='"+tabId+full.ccode+"' tips='"+full.ddesc+"' >"+full.ccode+"</span></p>";
			}
		}];
		var datas = [];
		datas = datas.concat(data);
		concatTableData(quotas, datas, "drgsTable-table", columns, "drgs", "dep",function(table, oSetting){
			var seriesdata_1_copy = jQuery.extend(true, [], seriesdata_1);
			var seriesdata_2_copy = jQuery.extend(true, [], seriesdata_2);
			var legend_copy = jQuery.extend(true, [], legend);
			var aoData = oSetting.aoData;
			table.find("tbody tr").each(function(i,n){
				$(n).find("td:last a").unbind();
				$(n).find("td:last a").click(function(){
					var rowIndex = $(this).attr("data-rowindex");
					if($("#"+tabId+" #lineChart-3 img").size()>0 || $("#"+tabId+" #lineChart-4 img").size()>0) return;
					if(seriesdata_1_copy.length<=0) seriesdata_1_copy = jQuery.extend(true, [], seriesdata_1);
					if(seriesdata_2_copy.length<=0) seriesdata_2_copy = jQuery.extend(true, [], seriesdata_2);
					var aData = aoData[rowIndex]._aData;
					var ccode = aData.ccode;
					if($(this).find("i").hasClass("trend-link")){
						$("#"+tabId+" #lineChart-3").html('<img src="images/pageloading.gif" />');
						$("#"+tabId+" #lineChart-4").html('<img src="images/pageloading.gif" />');
						array_remove(legend_copy, ccode);
						array_removeAttr(seriesdata_1_copy, "name", ccode);
						array_removeAttr(seriesdata_2_copy, "name", ccode);
						loadTrendChart("时间消耗指数", xdata_1, seriesdata_1_copy, legend_copy, $("#"+tabId+" #lineChart-3"));
						loadTrendChart("费用消耗指数", xdata_2, seriesdata_2_copy, legend_copy, $("#"+tabId+" #lineChart-4"));
						$(this).find("i").removeClass("trend-link");
					}else{
						$("#"+tabId+" #lineChart-3").html('<img src="images/pageloading.gif" />');
						$("#"+tabId+" #lineChart-4").html('<img src="images/pageloading.gif" />');
						var param_t = drgUI.setAjaxData(tabId);//页面参数
						param_t.monitorType = "drgs";
						param_t.pardrgCode = ccode;
						legend_copy.push(ccode);
						getTrendData(param_t, "TimeEff", function(data_0){
							var seriedata_avgdays = {type: "line", smooth: true, symbol:"none", name: ccode, data:[]};
							for(o in data_0){
								seriedata_avgdays.data.push(data_0[o].QuotaValue?data_0[o].QuotaValue:0);
							}
							seriesdata_1_copy.push(seriedata_avgdays);
							loadTrendChart("时间消耗指数", xdata_1, seriesdata_1_copy, legend_copy,  $("#"+tabId+" #lineChart-3"));
						})
						getTrendData(param_t, "FeeEff", function(data_1){
							var seriedata_avgfee = {type: "line", smooth: true, symbol:"none", name: ccode, data:[]};
							for(o in data_1){
								seriedata_avgfee.data.push(data_1[o].QuotaValue?data_1[o].QuotaValue:0);
							}
							seriesdata_2_copy.push(seriedata_avgfee);
							loadTrendChart("费用消耗指数", xdata_2, seriesdata_2_copy, legend_copy, $("#"+tabId+" #lineChart-4"));
						})
						$(this).find("i").addClass("trend-link");
					}
				});
			});
			table.find("i.trend-link").click();
		});
	})
	
	//添加超链接（病组结构分析） 点击监听事件
	var aHref = $("#"+tabId+"  #disGroupHref a");
	//传递最新的上级页面参数
	var myBingZuJieGouParam = $("#tabContent > .tab-pane.active").data(); 
	//设置要打开的病组结构分析的tab页唯一id值
	var aHrefTabId = 'disGroupAnalysis-'+myBingZuJieGouParam.depCode;
	aHref.unbind("click");
	aHref.bind("click", function(){
		//如果写成 var p =drgUI.setAjaxData(tabId); 则页面始终加载第一次打开时的数据
		var p = myBingZuJieGouParam;
		//父页面科室指标象限分析的tab标题为CMI和DRGs组数---跳转到医政角色病组结构分析页面
		if(p.patientGroupAnalysisPage && p.patientGroupAnalysisPage == 'yizheng'){
			openTab('/pages/yibao/fenxi/disStructureAnalysis.jsp', '病组结构分析'+"|"+p.depName,aHrefTabId, aHrefTabId,p);
		}else if(p.patientGroupAnalysisPage && p.patientGroupAnalysisPage == 'yibao'){
			//父页面科室指标象限分析的其他标题--跳转到医保角色病组结构分析页面
			openTab('/pages/yibao/fenxi/disGroupAnalysis.jsp', '病组结构分析'+"|"+p.depName,aHrefTabId, aHrefTabId,p);
		}
	});
	//	
	//将指标与数据拼接成datatables要输出的格式
	function concatTableData(newQuotas, datas, divId, columns, monitorType, specHeader,callback){
		var thead = $("#"+tabId+" #"+divId+" thead tr");
		//在病组页签中添加一列病组描述，并将该列设置为不可见，但数据导出可以导出
		if(monitorType == "drgs" ){
			thead.append("<td class='displayColumn' style='display: none;'>病组描述</td>");
			var descColumn = {
				"data": "ddesc","visible": false
			}
			columns.push(descColumn);
		}
		//循环拼接指标
		for(var o in newQuotas){
			var quota = newQuotas[o];
			if(quota.code=="ExcessCnt_TF"){
				thead.append("<td>" + quota.name + "<span class='drgtips'  tips='平均住院日或次均费用超标的病例' style='color: #0072ff;'>[注]</span></td>");
			}else if(quota.code=="SpecificCnt_TF"){
				thead.append("<td>" + quota.name + "<span class='drgtips'  tips='平均住院日超过1.2*时间消耗指数*平均住院日标杆的病例 或者 次均费用>1.2*费用消耗指数*次均费用标杆的病例' style='color: #0072ff;'>[注]</span></td>");
			}else if(quota.code=="TimeEff_TF"){
				thead.append("<td >" + quota.name + "<span class='drgtips'   tips='排除特异病例后的时间消耗指数' style='color: #0072ff;'>[注]</span></td>");
			}else if(quota.code=="FeeEff_TF"){
				thead.append("<td >" + quota.name + "<span class='drgtips'  tips='排除特异病例后的费用消耗指数' style='color: #0072ff;'>[注]</span></td>");
			}else{
				thead.append("<td >"+quota.name+"</td>");
			}
		}
		var i=0;
		configQuotaFlag(newQuotas[i].code,newQuotas[i].name,i,newQuotas,newQuotas[i].isLink);
		//对非基础指标列进行拼接，用递归方法解决循环与多个ajax请求异步问题
		function configQuotaFlag(value,name,cnt,array,isLink){
			if(value=="TimeEffcc"||value=="FeeEffcc"){
				columns.push({  "data": value,
								"render": function(obj, type, full){
										if(array[cnt].value==undefined||array[cnt].value==""){
											return "-";
										}else{
											return array[cnt].value;
										}
									}
					});
				if(cnt+1<array.length){
					configQuotaFlag(array[cnt+1].code,array[cnt+1].name,cnt+1,array,array[cnt+1].isLink);
				}
				else return false;
			}else{
				//对有病例明细的列添加链接
				if(isLink=="1"){
					columns.push({ "data": value,
						"type":"number-html",
						"render": function(obj, type, full){
							if(full[value]=="0"||full[value]==undefined){
								return "0";
							}
							return "<a href=\"javascript:loadCaseDtl('"+full.ddesc+"|"+name+"','"+full.ccode+"','"+monitorType+"', '"+value+"')\">"+(full[value]?full[value]:"")+"</a>";
						}
					});
				}
				else{
					if(value=="FeeEffContri"){
						columns.push({ "data": value, "bVisible": false,
							"render": function(obj, type, full){
								if(full[value]==undefined){
									return "-";

								}else{
									return full[value];
								}
							}});
					}else{
						columns.push({ "data": value, "bVisible": true,
							"render": function(obj, type, full){
								if(full[value]==undefined){
									return "-";

								}else{
									return full[value];
								}
							}});
					}
				}
				getTableData(value, monitorType, specHeader,function(data, quotaCode){
					var data_s = [];
					data_s = data_s.concat(data);
					mergeListcc(datas, data_s,'ccode','',quotaCode);
					ajaxCount--;
				})
				if(cnt+1<array.length){
					configQuotaFlag(array[cnt+1].code,array[cnt+1].name,cnt+1,array,array[cnt+1].isLink);
				}
				else return false	
			}
		}
			
		var timer = setInterval(function(){
			if(ajaxCount==0){
				clearInterval(timer);
				if(monitorType != "dep"){
					thead.append("<td>操作</td>");
					var operationColumn = {
						"data": "OPERATION",
		            	"render" : function(obj, type, full,meta) {
		            		var trendClass = "";
		            		if(monitorType == "dep"){
		            			trendClass = " trend-link";
		            		}
		            		return "<div><a class='tr' title='趋势' data-rowindex='"+meta.row+"'><i class='fa fa-line-chart"+trendClass+"'></a></i></div>";
		            	}
					}
					columns.push(operationColumn);
				}
				var sorti = columns.length-2;
				var tableTitle=$("#"+tabId+" #"+divId).find(".contaotalsccq").text();
				var orderDefs1=[[1,"desc"]];
				var orderDefs2=[[2,"desc"]];
				var paramsDataTables={lengthChange:false,info:false,order:[ [ sorti, "desc" ] ],searching:true,tableTitle:tableTitle,columnDefs:[{orderable:false,targets:[columns.length-1]}],order:["1","desc"]};
				var drawCallback= function(oSettings) {
					// 获取datatables标题,供导出时获取
					oSettings.expertName = this.find(".contaotalsccq").text();
					var table = $(this);
					callback(table, oSettings);
				}
				paramsDataTables.drawCallback=drawCallback;
				if(monitorType == "dep"){
					paramsDataTables["ordering"] = false;
					paramsDataTables.paging=false;
                    paramsDataTables.searching=false ;
				}
				if(monitorType == "drgs"){
					paramsDataTables["scrollY"] = "300px";
					paramsDataTables["order"] =orderDefs2;
				}else if(monitorType == "clinic"){
					paramsDataTables["scrollY"] = "400px";
					paramsDataTables["order"] =orderDefs1;
				}
				$("#"+tabId+" #"+divId).dataTableBasic(columns,datas,paramsDataTables);
				if(monitorType == "drgs"){
					$('.testCon').each(function(i,n){
						$(n).removeClass('testCon');
					});
				}
			}
		}, 500);
	}
	
	// 根据指标发送请求获取数据
	function getTableData(quotaCode, monitorType, specHeader,callback){
		var param = drgUI.setAjaxData(tabId);//页面参数
		param["monitorType"]=monitorType;
		param["specHeader"]=specHeader;
		param["quotaCode"]=quotaCode;
		param["isGetCc"]="0";
		$.post(contextPath + "/DrgsIndexCal/indexCal.dhc",param).done(function(data){
			var data = data.data;
			callback(data, quotaCode);
		})
	}
	
	//获取双指标趋势数据
	function getTrendData(param_t, quotaCode, callback){
		param_t.quotaCode = quotaCode;
		$.post(contextPath + "/ywcHome/getCMIDRGCount.dhc",param_t).done(function(data){
			var data = data.data;
			callback(data, quotaCode);
		})
	}
	
	function loadTrendChart(title, xdata, seriesdata, legend, container){
		//调公共方法组装数据
		var lines = [];
		var echartData=[];
		for(var o in legend){
			lines.push({code:'code'+o,desc:legend[o],data:[]});
		}
		for(var o in seriesdata){
			var obj=[];
			for(var j in seriesdata[o].data){
				var temp={};
				temp.ddesc=xdata[j];
				temp['code'+o]=seriesdata[o].data[j];
				obj.push(temp);
			}
			echartData.push(obj);
		}
		var chartParams={legendPosition:'bottom'}
		lineChartBasic(container, lines, echartData, title+'变化趋势','',chartParams);
	}
	
//	var quotas_tt = ["InCaseCnt", "DRGCount", "TimeEff", "FeeEff", "CMI"];
//	var param_tt = drgUI.setAjaxData(tabId);//页面参数
//	for(var o in quotas_tt){
//		var quotaCode = quotas_tt[o];
//		param_tt.quotaCode = quotaCode;
//		param_tt.type1 = "0";
//		getQuotas5(param_tt, o, function(data, o){
//			if(data && data.length>0){
//				$("#"+tabId+" #drgsTable-table-1").find("tbody tr:eq(0)").find("td").eq(Number(o)+1).html(data[0].QuotaValue);
//			}
//		})
//		param_tt.type1 = "1";
//		getQuotas5(param_tt, o, function(data, o){
//			if(data && data.length>0){
//				$("#"+tabId+" #drgsTable-table-1").find("tbody tr:eq(1)").find("td").eq(Number(o)+1).html(data[0].QuotaValue);
//			}
//		})
//	}
//	
//	function getQuotas5(param_tt, o, callback){
//		$.post(contextPath + "/DrgsIndexCal/indexCal.dhc", param_tt).done(function(data){
//			callback(data.data, o)
//		})
//	}
//	
//	$("#"+tabId+" #drgsTable-table-1").DataTable({
//		"destroy": true,
//		"paging": false, //翻页功能
//		"lengthChange": false, //改变每页显示数据数量
//		"searching": false, //过滤功能
//		"info": false, //页脚信息
//	    "stateSave": false, 
//	    "ordering": false,
//	    "pageLength": false
//	});
	
})

